<template>
  <div id="app">
    <header class="header">
      <h1>预定下单系统</h1>

      <nav class="nav-tabs">
        <button
          @click="setCurrentPage('products')"
          :class="{ active: currentPage === 'products' }"
          class="nav-tab"
        >
          商品列表
        </button>
        <button
          @click="setCurrentPage('reservations')"
          :class="{ active: currentPage === 'reservations' }"
          class="nav-tab"
        >
          我的预定
          <span v-if="pendingCount > 0" class="badge">{{ pendingCount }}</span>
        </button>
      </nav>

      <div class="header-info">
        <span class="user-info">欢迎，{{ currentUser.name }}</span>
      </div>
    </header>

    <main class="main">
      <!-- 商品列表页面 -->
      <ProductList
        v-if="currentPage === 'products'"
        :products="products"
        @reserve-product="handleReserveProduct"
      />

      <!-- 我的预定页面 -->
      <MyReservations
        v-if="currentPage === 'reservations'"
        :reservations="userReservations"
        @confirm-reservation="confirmReservation"
        @cancel-reservation="cancelReservation"
        @complete-reservation="completeReservation"
        @go-to-products="setCurrentPage('products')"
      />

      <!-- 预定确认模态框 -->
      <ReservationModal
        v-if="showReservationModal"
        :product="selectedProduct"
        :quantity="selectedQuantity"
        @close="closeReservationModal"
        @submit-reservation="submitReservation"
      />

      <!-- 加载状态 -->
      <LoadingSpinner
        v-if="isLoading"
        overlay
        :message="loadingMessage"
      />

      <!-- Toast容器 -->
      <ToastContainer />
    </main>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import ProductList from './components/ProductList.vue'
import MyReservations from './components/MyReservations.vue'
import ReservationModal from './components/ReservationModal.vue'
import LoadingSpinner from './components/LoadingSpinner.vue'
import ToastContainer from './components/ToastContainer.vue'
import { state, actions, getters } from './store/index.js'
import { toast } from './utils/toast.js'

// 响应式引用store中的数据
const products = computed(() => state.products)
const currentPage = computed(() => state.currentPage)
const currentUser = computed(() => state.currentUser)
const userReservations = computed(() => getters.userReservations.value)
const pendingCount = computed(() => getters.pendingReservationsCount.value)

// 本地状态
const showReservationModal = ref(false)
const selectedProduct = ref(null)
const selectedQuantity = ref(1)
const isLoading = ref(false)
const loadingMessage = ref('')

// 页面切换
const setCurrentPage = (page) => {
  actions.setCurrentPage(page)
}

// 处理预定商品
const handleReserveProduct = ({ product, quantity }) => {
  selectedProduct.value = product
  selectedQuantity.value = quantity
  showReservationModal.value = true
}

// 关闭预定模态框
const closeReservationModal = () => {
  showReservationModal.value = false
  selectedProduct.value = null
  selectedQuantity.value = 1
}

// 提交预定
const submitReservation = async (reservationData) => {
  isLoading.value = true
  loadingMessage.value = '正在提交预定...'

  try {
    await actions.createReservation(
      reservationData.product.id,
      reservationData.quantity,
      reservationData.userInfo
    )

    closeReservationModal()

    toast.success('预定成功！', {
      title: '预定成功',
      message: '您的预定已提交，请在"我的预定"中查看详情'
    })

  } catch (error) {
    toast.error(error.message, {
      title: '预定失败',
      duration: 5000
    })
  } finally {
    isLoading.value = false
    loadingMessage.value = ''
  }
}

// 确认预定
const confirmReservation = async (reservationId) => {
  try {
    await actions.confirmReservation(reservationId)
    toast.success('预定已确认')
  } catch (error) {
    toast.error('确认预定失败: ' + error.message)
  }
}

// 取消预定
const cancelReservation = async (reservationId) => {
  try {
    await actions.cancelReservation(reservationId)
    toast.info('预定已取消，库存已释放')
  } catch (error) {
    toast.error('取消预定失败: ' + error.message)
  }
}

// 完成预定
const completeReservation = async (reservationId) => {
  try {
    await actions.completeReservation(reservationId)
    toast.success('预定已完成')
  } catch (error) {
    toast.error('操作失败: ' + error.message)
  }
}

// 初始化数据
onMounted(async () => {
  isLoading.value = true
  loadingMessage.value = '正在加载数据...'

  try {
    await actions.initializeData()
  } catch (error) {
    console.error('初始化数据失败:', error)
    toast.error('加载数据失败，请刷新页面重试')
  } finally {
    isLoading.value = false
    loadingMessage.value = ''
  }
})
</script>

<style>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
  background: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background: white;
  border-bottom: 1px solid #dee2e6;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header h1 {
  margin: 0;
  color: #333;
  font-size: 1.5rem;
}

.nav-tabs {
  display: flex;
  gap: 0.5rem;
}

.nav-tab {
  padding: 0.75rem 1rem;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 6px;
  color: #666;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.2s;
  position: relative;
}

.nav-tab:hover {
  background: #f8f9fa;
  color: #007bff;
}

.nav-tab.active {
  background: #007bff;
  color: white;
}

.badge {
  background: #dc3545;
  color: white;
  border-radius: 10px;
  padding: 0.2rem 0.5rem;
  font-size: 0.7rem;
  min-width: 18px;
  text-align: center;
}

.nav-tab.active .badge {
  background: white;
  color: #007bff;
}

.header-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.user-info {
  color: #666;
  font-size: 0.9rem;
}

.main {
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 80px);
}



@media (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }

  .nav-tabs {
    width: 100%;
    justify-content: center;
  }

  .main {
    padding: 1rem;
  }
}
</style>